//
//  MyCollectionViewController.m
//  OpenYourEyes
//
//  Created by 沈振烨 on 15/8/14.
//  Copyright (c) 2015年 zhanglei. All rights reserved.
//

#import "MyCollectionViewController.h"
#import "FMDB.h"
#import "DailyModel.h"
#import "MyCollectionTableViewCell.h"
#import "SingleViewController.h"

@interface MyCollectionViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *array;  // 接收数据库的数组

@property (nonatomic, strong)FMDatabase *db;


@end

@implementation MyCollectionViewController

- (void)dealloc
{
    NSLog(@"已经被销毁");
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self readPath];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"我的收藏";
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    [self createTableView];
    
}

/**  创建tableview */
- (void)createTableView
{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - 64) style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [_tableView registerClass:[MyCollectionTableViewCell class] forCellReuseIdentifier:@"daily"];
    [self.view addSubview:_tableView];
    
}

/** 数据库读取 */
- (void)readPath
{
    _array = [NSMutableArray array];
    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:@"Collection.db"];
    _db = [FMDatabase databaseWithPath:path];
    [_db open];
    NSLog(@"++++++%@",path);
    FMResultSet *rs = [_db executeQuery:@"SELECT * FROM Collection"];
    while ([rs next])  //  类库中的方法  循环查询
    {
        
        NSString *category = [rs stringForColumn:@"category"];
        NSString *coverBlurred = [rs stringForColumn:@"coverBlurred"];
        NSString *coverForDetail = [rs stringForColumn:@"coverForDetail"];
        NSString *coverForFeed = [rs stringForColumn:@"coverForFeed"];
        NSString *coverForSharing = [rs stringForColumn:@"coverForSharing"];
        NSString *Description = [rs stringForColumn:@"Description"];
        NSString *playUrl = [rs stringForColumn:@"playUrl"];
        NSString *rawWebUrl = [rs stringForColumn:@"rawWebUrl"];
        NSString *title = [rs stringForColumn:@"title"];
        NSString *webUrl = [rs stringForColumn:@"webUrl"];
        
        int ID = [rs intForColumn:@"ID"];
        int idx = [rs intForColumn:@"idx"];
        int date = [rs intForColumn:@"date"];
        int duration = [rs intForColumn:@"duration"];
        
        DailyModel *model = [[DailyModel alloc] init];
        model.category = category;
        model.coverBlurred = coverBlurred;
        model.coverForDetail = coverForDetail;
        model.coverForFeed = coverForFeed;
        model.coverForSharing = coverForSharing;
        model.Description = Description;
        model.playUrl = playUrl;
        model.rawWebUrl = rawWebUrl;
        model.title = title;
        model.webUrl = webUrl;
        
        model.ID = ID;
        model.idx = idx;
        model.date = date;
        model.duration = duration;
        
        [_array addObject:model];
    }
    [rs close];

    
}


- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    // 让tableView在编辑状态和非编辑状态之间进行切换
    [_tableView setEditing:editing animated:animated];
}

//   确定哪些行可以进行编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

//  返回编辑类型(插入,删除)
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}


//   编辑结束后需要做什么事情(不管是插入还是删除,都需要先删除数据源,然后再删除cell)
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    //  编辑删除,先删除数据库中的文件
    DailyModel *model = _array[indexPath.row];
    [self deleteDataWithTitle:model.title];
    //  再将数组中得数据删掉
    [_array removeObjectAtIndex:indexPath.row];
    //  再将cell删除掉
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];    
}

#pragma park - 封装一个删除数据库内容的方法
- (void)deleteDataWithTitle:(NSString *)string
{
    //  按title 查询删除
    //  删除数据库的方法
    NSString *deleteStr = [NSString stringWithFormat:@"DELETE fROM %@ where %@ = '%@'",@"Collection",@"Title",string];
    [_db executeUpdate:deleteStr];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 240;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MyCollectionTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"daily" forIndexPath:indexPath];
    DailyModel *model = _array[indexPath.row];
    cell.model = model;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 跳转到详情界面
    DailyModel *model = _array[indexPath.row];
    SingleViewController *singleVC = [[SingleViewController alloc] init];
    singleVC.model = model;
    [self.navigationController pushViewController:singleVC animated:YES];
}









/**
 *  添加右导航栏按钮
 */
//- (void)addnavigationRightButton
//{
//    _rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
//    _rightButton.frame = CGRectMake(7, 7, 30, 30);
//    [_rightButton setImage:[UIImage imageNamed:@"iconfont-bianji"] forState:UIControlStateNormal];
//    [_rightButton addTarget:self action:@selector(doTapToEdit:) forControlEvents:UIControlEventTouchUpInside];
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_rightButton];
//    self.navigationController.navigationBar.translucent = NO;
//    _isEdit = YES;
//    
//}


//- (void)doTapToEdit:(UIButton *)sender
//{
//    if (_isEdit == YES)
//    {
//        [_rightButton setImage:[UIImage imageNamed:@"iconfont-cancel"] forState:UIControlStateNormal];
//        
//    } else
//    {
//        [_rightButton setImage:[UIImage imageNamed:@"iconfont-bianji"] forState:UIControlStateNormal];
//    }
//    
//    
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
